package _20250227;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

public class ThirtyYearOld {
    /**
     * 3.现有一个List<Person>，Person类有String name和int age两个属性。
       使用 Stream 流筛选出年龄大于 30 岁的人，并按姓名的字母顺序和年龄顺序输出他们的姓名和年龄。
     */
    public static void main(String[] args)  {
        ArrayList<Person> personList1 = new ArrayList<>();
        personList1.add(new Person("Alice", 25));
        personList1.add(new Person("Bob", 35));
        personList1.add(new Person("Charlie", 40));
        personList1.add(new Person("David", 22));
        personList1.add(new Person("Eve", 32));

// 测试数据 2：所有人年龄都小于 30 岁
        ArrayList<Person> personList2 = new ArrayList<>();
        personList2.add(new Person("Alice", 25));
        personList2.add(new Person("Bob", 28));

// 测试数据 3：空列表
        ArrayList<Person> personList3 = new ArrayList<>();

// 测试数据 4：姓名相同但年龄不同
        ArrayList<Person> personList4 = new ArrayList<>();
        personList4.add(new Person("Alice", 35));
        personList4.add(new Person("Alice", 40));

// 测试
        System.out.println(exceedThirtyYearOld(personList1));
        System.out.println(exceedThirtyYearOld(personList2));
        System.out.println(exceedThirtyYearOld(personList3));
        System.out.println(exceedThirtyYearOld(personList4));
    }

    /**
     * 排序，找出大于30岁的工作人员
     * @param list 所有工作人员信息
     * @return 返回列表
     */
    private static List<Person> exceedThirtyYearOld(ArrayList<Person> list) {
        if(list.isEmpty()){
            return Collections.emptyList();

        }
            return list.stream()
                    .filter(s->s.getAge()>30)
                    .sorted(Comparator.comparing(Person::getName).thenComparing(Person::getAge))
                    .collect(Collectors.toList());

    }

}

/**
 *构造person类
 */
 class Person{
     private String name;
     private int age;

     @Override
     public String toString() {
         return "Person{" +
                 "name='" + name + '\'' +
                 ", age=" + age +
                 '}';
     }

     public Person(String name, int age) {
         this.name = name;
         this.age = age;
     }

     public String getName() {
         return name;
     }

     public void setName(String name) {
         this.name = name;
     }

     public int getAge() {
         return age;
     }

     public void setAge(int age) {
         this.age = age;
     }
 }
